package com.yanqu.road.server.manager.tomb.event;

import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.manager.tomb.Position;
import com.yanqu.road.server.manager.tomb.TombMaze;
import com.yanqu.road.logic.activity.tomb.config.entity.ActivityTombEvent;
import com.yanqu.road.server.manager.tomb.entity.DbTombLog;
import com.yanqu.road.server.manager.tomb.entity.DbTombPosition;
import com.yanqu.road.server.manager.tomb.entity.DbTombUser;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class Choose implements IEvent {
    @Override
    public boolean canStand(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return true;
        }
        long unionDigUser = dbTombPosition.acquireUnionDigUser(dbTombUser.getUserInfo().getUnionUid());
        if (unionDigUser != dbTombUser.getUserId()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean canThrough(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return true;
    }

    @Override
    public boolean canExplore(DbTombUser dbTombUser, TombMaze tombMaze, Position position) {
        return false;
    }

    @Override
    public void detail(TombMaze tombMaze, DbTombUser dbTombUser, Position position, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        long unionDigUser = dbTombPosition.acquireDigUser();
        if (unionDigUser != dbTombUser.getUserId()) {
            return;
        }
        ActivityTombEvent activityTombEvent = tombMaze.getConfig().getEvent(dbTombPosition.getEventId());
        DbTombUser digDbTombUser = tombMaze.getActivity().getDbData().getDbTombUser(unionDigUser);
        positionTempBuilder.setDigUser(digDbTombUser.toEventUserInfoBuilder());
        positionTempBuilder.setEventConfig(activityTombEvent.getEventConfigBuilder());
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
    }

    @Override
    public void explore(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventExploreResp.Builder builder) {
        dbTombUser.setLockEvent(position.getKey());
        dbTombUser.refreshUpdateTime();
        positionTempBuilder.setEnum(position.getpEnum());
        positionTempBuilder.setEventEnum(position.geteType());
    }

    @Override
    public void display(DbTombUser dbTombUser, TombMaze tombMaze, Position position, Tomb.PositionTemp.Builder p) {
        DbTombPosition dbTombPosition = tombMaze.getDbTombPosition(position.getKey());
        if (dbTombPosition == null) {
            return;
        }
        long unionDigUser = dbTombPosition.acquireUnionDigUser(dbTombUser.getUserInfo().getUnionUid());
        if (unionDigUser != dbTombUser.getUserId()) {
            return;
        }
        p.setIsOver(false);
    }


    @Override
    public void info(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventInfoResp.Builder builder) {
        long unionDigUser = dbTombPosition.acquireDigUser();
        if (unionDigUser == 0) {
            return;
        }
        positionTempBuilder.setEnum(Tomb.PositionEnum.POSITION_EVENT_VALUE);
        DbTombUser digDbTombUser = tombMaze.getActivity().getDbData().getDbTombUser(unionDigUser);
        positionTempBuilder.setDigUser(digDbTombUser.toEventUserInfoBuilder());
        positionTempBuilder.setEventConfig(activityTombEvent.getEventConfigBuilder());
    }

    @Override
    public void touch(TombMaze tombMaze, DbTombUser dbTombUser, DbTombPosition dbTombPosition, Position position, ActivityTombEvent activityTombEvent, Tomb.PositionTemp.Builder positionTempBuilder, Tomb.TombEventTouchResp.Builder builder, Tomb.TombEventTouchReq req) {
        if (req.getRelationId() != 0 && req.getRelationId() != 1) {
            builder.setRet(GameErrorCode.E_ERROR_PARAMETER);
            return;
        }

        int isRight = 0;
        List<Integer> param4 = activityTombEvent.getParam4();
        for (int i = 0; i < param4.size(); i++) {
            if (param4.get(i) == 1) {
                if (req.getRelationId() == i) {
                    builder.setChoseEventFinish(activityTombEvent.getId());
                    isRight = 1;
                }
            }
        }

//        //日志
//        DbTombLog dbTombLog = DbTombLog.newInstance();
//        dbTombLog.setUid(String.valueOf(dbTombUser.getUserId()));
//        dbTombLog.setFloor(tombMaze.getFloor());
//        dbTombLog.setFromUserId(dbTombUser.getUserId());
//        dbTombLog.setLogEnum(Tomb.TombLogEnum.TombLogEnum_Buff_VALUE);
//        dbTombLog.setEventId(activityTombEvent.getId());
//        dbTombLog.setValue1(req.getRelationId());
//        dbTombLog.setRewards(builder.getRewards());
//        dbTombLog.setEnergyChange(0);
//        dbTombLog.setEnergy(dbTombUser.getEnergy());
//        dbTombLog.setX(position.getRow());
//        dbTombLog.setY(position.getColumn());
//        dbTombLog.setDescription("二选一事件");
//        tombMaze.getActivity().getTombLog().add(dbTombLog);

        Property property = new Property(activityTombEvent.getParam3().get(req.getRelationId()));
        tombMaze.addProperty(property, dbTombUser);
        builder.setRewards(PropertyHelper.parsePropertyToString(property));
        tombMaze.updateGrip(position);
        dbTombPosition.setDone(1);
        position.setpEnum(Tomb.PositionEnum.POSITION_EVENT_DONE_VALUE);
        dbTombPosition.refreshUpdateTime();

        dbTombUser.setLockEvent("");
        dbTombUser.refreshUpdateTime();

        tombMaze.eventDone(dbTombPosition, position);

        //发到游戏服写日志
        Tomb.TombLogCrossToGameReq.Builder logBuilder = Tomb.TombLogCrossToGameReq.newBuilder();
        logBuilder.setLEnum(Tomb.GameTombLogEnum.CHOSE);
        logBuilder.setUserId(dbTombUser.getUserId());
        logBuilder.setActivityId(tombMaze.getActivityId());
        logBuilder.setUnionUid(dbTombUser.getUserInfo().getUnionUid());
        logBuilder.setFloor(dbTombUser.getFloor());
        logBuilder.setX(position.getRow());
        logBuilder.setY(position.getColumn());
        logBuilder.setHp(dbTombUser.getHp());
        logBuilder.setTimestamp(System.currentTimeMillis());
        Tomb.TombLogChose.Builder chose = Tomb.TombLogChose.newBuilder();
        chose.setRelationId(req.getRelationId());
        chose.setIsRight(isRight);
        chose.setRewards(builder.getRewards());
        logBuilder.setChose(chose);
        tombMaze.getActivity().sendLogToGame(dbTombUser, logBuilder);
    }
}
